//
//  shangpinxqViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/2.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "OALoginViewController.h"
#import "fajianxqViewController.h"
#import "qiyexqViewController.h"
#import "comnewsTableViewCell.h"
#import "shangpinxqViewController.h"
#import "pdnewsTableViewCell.h"
#import "firstproductxqTableViewCell.h"
@interface shangpinxqViewController ()

@end

@implementation shangpinxqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self requestdata];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];

self.extendedLayoutIncludesOpaqueBars = YES;

if (@available(iOS 11.0, *)) {

UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

} else {

    self.automaticallyAdjustsScrollViewInsets = NO;

}


}
- (void)backaction{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    self.navigationController.navigationBarHidden = YES;
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
self.automaticallyAdjustsScrollViewInsets = NO;

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    self.navigationController.navigationBarHidden = NO;

}

- (void)requestdata{
    NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
  dics[@"pdid"] = _pdid;

  [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *dicstring = [httpTool dictionaryToJson:dics];
//dicstring= [dicstring URLDecodedString];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/product/yellowpage?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  [MBProgressHUD hideHUDForView:self.view animated:YES];
  _dic = dictionary;
  _imagearray = dictionary[@"pdimage"];
  _productdic = dictionary[@"product"];
  _companydic = dictionary[@"pdcompany"];
[self initview];
NSLog(@"%@",dictionary);
 }andFailure:^{
  [MBProgressHUD hideHUDForView:self.view animated:YES];

//         [self.tableView headerEndRefreshing];

 }];

}
- (void)initview{
if(!_tableView){
 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.bounces = NO;
_tableView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
[self.view addSubview:_tableView];
 [_tableView registerNib:[UINib nibWithNibName:@"comnewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"comnewsTableViewCell"];
  [_tableView registerNib:[UINib nibWithNibName:@"pdnewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"pdnewsTableViewCell"];

UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.frame = CGRectMake(10, 20, 40, 40);
//[backbutton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
[backbutton setImage:[UIImage imageNamed:@"灰色返回"] forState:UIControlStateNormal];

[backbutton addTarget:self action:@selector(backaction) forControlEvents:UIControlEventTouchUpInside];
[self.tableView addSubview:backbutton];

}

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//return 5;
if (section==0) {
    return 3;
}else if (section ==1){
 if (self.isExpand) {
            return 1;
        }else{
            return 0;
        }
}else{
return 1;
}

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
    

if (indexPath.row ==0) {
         static NSString *CellIdentifier0 = @"Cellbendititlepic0";
firstproductxqTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
if (!cell) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell = [[firstproductxqTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
cell.dict = _productdic;
cell.array = _imagearray;
return cell;
}else if (indexPath.row ==1){
 comnewsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"comnewsTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell = [[[UINib nibWithNibName:@"comnewsTableViewCell" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    cell.dict =_companydic;
    return cell;


}else {
 static NSString *CellIdentifier2 = @"Cellbendititlepic2";

    pdnewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
if (cell==nil) {
        cell=[[pdnewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
cell.dic = _dic;
    return cell;

}
} else if (indexPath.section ==1){
 static NSString *CellIdentifier3 = @"Cellbendititlepic3";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
//    UIView *view  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
//    view.backgroundColor  = [UIColor colorWithHexString:@"f0eff5"];
//    [cell.contentView addSubview:view];
////    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 1, KScreenWidth, 29)];
////    label.backgroundColor = [UIColor whiteColor];
////    label.textAlignment = 1;
////    label.textColor =[UIColor colorWithHexString:@"0a62ea"];
////    label.text = NSLocalizedString(@"商品详情-详细信息", nil);
////    label.font = [UIFont systemFontOfSize:14];
////        [cell.contentView addSubview:label];
//UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.frame = CGRectMake(0, 1, KScreenWidth, 29);
//    button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
//
//[button setTitle:NSLocalizedString(@"商品详情-详细信息", nil) forState:UIControlStateNormal];
//   [button setTitleColor:[UIColor colorWithHexString:@"0a62ea"]forState:UIControlStateNormal];
//[button addTarget:self action:@selector(xqaction:) forControlEvents:UIControlEventTouchUpInside];
//[cell.contentView addSubview:button];
        NSString *string = _productdic [@"newDescription"];
          CGFloat height =  [string boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;

    UILabel *textlabel = [[UILabel alloc]init];
textlabel.frame = CGRectMake(10, 30, KScreenWidth-20, height);
textlabel.numberOfLines = 0;
    textlabel.backgroundColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:13];
    textlabel.textColor =[UIColor colorWithHexString:@"a1a1a1"];
textlabel.text = string;
[cell.contentView addSubview:textlabel];
if (_isExpand) {
    textlabel.hidden = NO;
}else{
    textlabel.hidden = YES;

}
    return cell;

}else {
 static NSString *CellIdentifier4 = @"Cellbendititlepic4";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
if (cell==nil) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
             UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
      button1.frame = CGRectMake(10, 20, (KScreenWidth-40)/2, 40);
          button1.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];

  [button1 setTitle:NSLocalizedString(@"商品详情-发送联系信", nil) forState:UIControlStateNormal];
    button1.titleLabel.font = [UIFont systemFontOfSize: 14.0];
   [button1 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
 [button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];

            [cell.contentView addSubview:button1];
             UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
      button2.frame = CGRectMake(button1.right +20, 20, (KScreenWidth-40)/2, 40);
          button2.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

  [button2 setTitle:NSLocalizedString(@"商品详情-加入购物车", nil) forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize: 14.0];
   [button2 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
 [button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];

            [cell.contentView addSubview:button2];
    return cell;

}



}
- (void)xqaction:(UIButton *)button {
    self.isExpand=!self.isExpand;

//if (button.selected) {
//    _type = @"stay";
//    button.selected = NO;
//}else{
//    _type = @"nostay";
//    button.selected = YES;

//}
//button.selected = !button.selected;

[_tableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {

if (indexPath.row ==0) {
    return 150+70+10+20;
}else if(indexPath.row ==1){
//return 140+5+5+20+30+10+60+12;
NSDictionary *dic =_companydic;
     CGFloat height = [OAToolBox sizebyText:dic[@"address"] font:13 minsWidth:100];
          CGFloat height2 = [OAToolBox sizebyText:dic[@"websites"] font:13 minsWidth:100];
//    return 140+5+5+20+30+10+60+12+15+15;

     if ((height>31.5)&&(height2>31.5)) {
    return 140+5+5+20+30+10+60+12+15+15;
}else if(height>31.5){
   return 140+5+5+20+30+10+60+12+15;

}else if(height2>31.5){
    return 140+5+5+20+30+10+60+12+15;

}else{
return 140+5+5+20+30+10+60+12;
}

// self.tableView.estimatedRowHeight = 150;//估算高度
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
}else{
NSArray *pricearray  =_dic[@"pdprice"];
NSArray *attributearray  =_dic[@"pdattribute"];
NSDictionary *dic  =_dic[@"product"];
     CGFloat height = [OAToolBox sizebyText:dic[@"keywords"] font:13 minsWidth:120];

if (pricearray.count==0) {
return height+3*32+attributearray.count*32+30;
  

}else{
return height+2*32+attributearray.count*32+pricearray.count*32+30;


}
}
}
else if(indexPath.section ==1){
NSString *string = _productdic [@"newDescription"];
          CGFloat height =  [string boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
if (_isExpand) {
    return height+30+10;

}else{
return 30;
}


}

else {

return 70;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 if (section==1) {
         UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
         bgView.backgroundColor = [UIColor whiteColor];
    UIView *view  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
    view.backgroundColor  = [UIColor colorWithHexString:@"f0eff5"];
    [bgView addSubview:view];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 1, KScreenWidth, 29);
    button.titleLabel.font = [UIFont systemFontOfSize: 14.0];

[button setTitle:NSLocalizedString(@"商品详情-详细信息", nil) forState:UIControlStateNormal];
   [button setTitleColor:[UIColor colorWithHexString:@"0a62ea"]forState:UIControlStateNormal];
[button addTarget:self action:@selector(xqaction:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:button];
     
return bgView;
 }else{
 return nil;
 }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return 30;
    }
    return 0;
}
- (void)action1
{      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]&&![[DanLi sharedDanLi].stay isEqualToString:@"yes"]) {
      OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationController.navigationBar.hidden = YES;
                 self.view.window.rootViewController = navVC;

}else{
fajianxqViewController *vc = [[fajianxqViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;

//@property (strong, nonatomic)  NSString *toMemberCompanyId;
//@property (strong, nonatomic)  NSString *toMemberNameId;
//
//@property (strong, nonatomic)  NSString *productId;

//vc.ipid = _pdid;
vc.productId = _pdid;
vc.toMemberNameId = _productdic[@"uid"];

vc.toMemberCompanyId = _companydic[@"id"];
[self.navigationController pushViewController:vc animated:YES];

}
}
- (void)action2{
   UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]&&![[DanLi sharedDanLi].stay isEqualToString:@"yes"]) {
      OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationController.navigationBar.hidden = YES;
                 self.view.window.rootViewController = navVC;

}else{
{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];
NSString *cpid = [[NSUserDefaults standardUserDefaults] objectForKey:@"cpid"];

NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid><cpid>%@</cpid><pid>%@</pid></body></message>",userName,password,website,language,BAtype,uid,cpid,_productdic[@"id"]];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bCart];
 [HttpTools requestWithURL:path arrays:array method:@"addCart" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
//[_tableView reloadData];

 } failure:^(NSError *error) {

    }];

}

}



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
